package com.example.demozmq.zmq;

import java.nio.charset.StandardCharsets;
import org.zeromq.ZMQ;

public class ZmqSubscriber {

    public static void main(String[] args) {
        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket socket = context.socket(ZMQ.SUB);
        // 连接服务端
        socket.connect("tcp://localhost:5556");
        // 订阅主题的value
        socket.subscribe("value".getBytes(StandardCharsets.UTF_8));

        while (true) {
            // 从通道上接收消息
            byte[] topic = socket.recv();
            byte[] data = socket.recv();
            int value = Integer.parseInt(new String(data));
            System.out.println("订阅的主题：" + new String(topic));
            System.out.println(System.currentTimeMillis() + "接收到的整数：" + value);
        }
    }

}
